﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityModel;
using BusinessLayer;
using System.Drawing;

namespace PL___Web_Application
{
    public partial class BusinessAccount : PagesMethods
    {
        private Business loggedBusiness;
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckCookie();
            loggedBusiness = GetBusinessFromCookie();
            BL controller = new BL();
            Session["LeasedTrucks"] = controller.leasedTruckList(GetBusinessFromCookie());

            if (!Page.IsPostBack)
            {
                username_b_tb.Text = loggedBusiness.Customer.username;
                password_b_tb.Text = loggedBusiness.Customer.password;
                phone_b_tb.Text = loggedBusiness.Customer.phone.ToString();
                address_b_tb.Text = loggedBusiness.Customer.address_c;
                company_id_tb.Text = loggedBusiness.b_identifier;
                company_name_tb.Text = loggedBusiness.company_name;
                contact_person_tb.Text = loggedBusiness.contact_person;
                fax_tb.Text = loggedBusiness.fax.ToString();
                
            }
        }
        protected void save_Business (object sender, EventArgs e)
        {
            loggedBusiness.Customer.username = username_b_tb.Text;
            loggedBusiness.Customer.password = password_b_tb.Text;
            loggedBusiness.company_name = company_name_tb.Text;
            loggedBusiness.b_identifier = company_id_tb.Text;
            loggedBusiness.contact_person = contact_person_tb.Text;
            loggedBusiness.fax = Convert.ToInt32(fax_tb.Text);
            loggedBusiness.Customer.phone = Convert.ToInt32(phone_b_tb.Text);
            loggedBusiness.Customer.address_c = address_b_tb.Text;

            try
            {
                BL controller = new BL();
                controller.updateBusinessCustomer(loggedBusiness);
                error_lb.ForeColor = Color.Blue;
                error_lb.Text = "Your data was correctly updated";
            }
            catch (Exception ex) { error_lb.Text = ex.Message; }

        }

        public List<Truck> leasedTruckList()
        {
            return (List<Truck>)Session["LeasedTrucks"];

        }
    }
}